﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace SageKing.Core.Contracts;


/// <summary>
/// 全局返回结果
/// httpclient调用返回结果
/// </summary>
/// <typeparam name="T"></typeparam>
public class SageKingResult<T>
{
    /// <summary>
    /// 状态码
    /// </summary>
    public int Code { get; set; }

    /// <summary>
    /// 类型success、warning、error
    /// </summary>
    public SageKingResultTypeEnum Type { get; set; }

    /// <summary>
    /// 错误信息
    /// </summary>
    public string Message { get; set; }

    /// <summary>
    /// 数据
    /// </summary>
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public T Result { get; set; }

    /// <summary>
    /// 附加数据
    /// </summary>
    public object Extras { get; set; }

    /// <summary>
    /// 时间
    /// </summary>
    public DateTime Time { get; set; }

    public bool IsSuccess
    {
        get
        {
            if (Code == 200 && Type == SageKingResultTypeEnum.Success)
            {
                return true;
            }
            return false;
        }
    }

    public SageKingResult<T> Ok(T result, string message = "操作成功", int code = (int)HttpStatusCode.OK)
    {
        Code = code;
        Type = SageKingResultTypeEnum.Success;
        Message = message;
        Result = result;
        Time = DateTime.UtcNow;
        return this;
    }
    public SageKingResult<T> NotFound(string message = "操作失败:NotFound", int code = (int)HttpStatusCode.NotFound)
    {
        Code = code;
        Type = SageKingResultTypeEnum.Error;
        Message = message;
        Time = DateTime.UtcNow;
        return this;
    }

    public SageKingResult<T> BadRequest(string message = "操作失败:BadRequest", int code = (int)HttpStatusCode.BadRequest)
    {
        Code = code;
        Type = SageKingResultTypeEnum.Error;
        Message = message;
        Time = DateTime.UtcNow;
        return this;
    }

    public SageKingResult<T> BadRequest(T result, string message = "操作失败:BadRequest", int code = (int)HttpStatusCode.BadRequest)
    {
        Result = result;
        BadRequest(message, code);
        return this;
    }

    public SageKingResult<T> ServerError(string message = "操作失败:InternalServerError", int code = (int)HttpStatusCode.InternalServerError)
    {
        Code = code;
        Type = SageKingResultTypeEnum.Error;
        Message = message;
        Time = DateTime.UtcNow;
        return this;
    }
    public SageKingResult<T> ServerError(T result, string message = "操作失败:InternalServerError", int code = (int)HttpStatusCode.InternalServerError)
    {

        Result = result;
        ServerError(message, code);
        return this;
    }
    public SageKingResult<T> Warning(string message = "操作警告", int code = (int)HttpStatusCode.MultipleChoices)
    {
        Code = code;
        Type = SageKingResultTypeEnum.Warning;
        Message = message;
        Time = DateTime.UtcNow;
        return this;
    } 
}
